#define  _CRT_SECURE_NO_WARNINGS
#include "Stack.h"

void print(Stack* ps)
{
	for (int i = 0; i < ps->top; i++)
	{
		printf("%d->", ps->arr[i]);
	}
	printf("\n");
}
//
void STInit(Stack* ps)
{
	assert(ps);
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
}

void STDestroy(Stack* ps)
{
	assert(ps);
	free(ps->arr);
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
}

void STPush(Stack* ps, int x)
{
	assert(ps);
	if (ps->capacity == ps->top)
	{
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDataType* tmp = (STDataType*)realloc(ps->arr,sizeof(STDataType) * newcapacity);
		assert(tmp);
		ps->arr = tmp;
		ps->capacity = newcapacity;
	}
	ps->arr[ps->top++] = x;
}

void STPop(Stack* ps)
{
	assert(ps);
	assert(ps->top);
	ps->top--;
}

STDataType STTop(Stack* ps)
{
	assert(ps);
	assert(ps->top);

	return ps->arr[--ps->top];
}

bool STEmpty(Stack* ps)
{
	assert(ps);
	return ps->top;
}